package com.example.android427;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class guotugonggaoActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guotugonggao);
        ListView mListView =  findViewById(R.id.gonggao_list);
        // 添加ListItem，设置事件响应
        mListView.setAdapter(new DemoListAdapter());
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int index,
                                    long arg3) {
                onListItemClick(index);
            }
        });
    }
    void onListItemClick(int index) {
        Intent intent = null;
        intent = new Intent(guotugonggaoActivity.this, demos[index].demoClass);
        this.startActivity(intent);
    }

    private static final DemoInfo[] demos = {
            new DemoInfo("国家图书馆第十六届文津图书奖正式启动","2020-12-10",R.drawable.gonggao, gonggaoxianqingActivity.class),
            new DemoInfo("国家图书馆第十六届文津图书奖正式启动","2020-12-10",R.drawable.gonggao, gonggaoxianqingActivity.class),
            new DemoInfo("国家图书馆关于面向社会各界征集“十四五”发展建议的公告","2020-10-15",R.drawable.gonggao,gonggaoxianqingActivity.class),
            new DemoInfo("国家图书馆关于面向社会各界征集“十四五”发展建议的公告","2020-10-15",R.drawable.gonggao,gonggaoxianqingActivity.class),
            new DemoInfo("国家图书馆有序恢复开馆读者指南","2020-09-27",R.drawable.gonggao,gonggaoxianqingActivity.class),
            new DemoInfo("国家图书馆关于进一步扩大开放区域的公告","2020-09-27",R.drawable.gonggao,gonggaoxianqingActivity.class),
            new DemoInfo("国家图书馆2020年国庆节、中秋节开馆安排","2020-09-27",R.drawable.gonggao,gonggaoxianqingActivity.class),
            new DemoInfo("“日本细菌战资源库”上线发布","2020-09-04",R.drawable.gonggao,gonggaoxianqingActivity.class),
            new DemoInfo("全部上线：国图公开课《诗词格律十二讲》","2020-08-19",R.drawable.gonggao,gonggaoxianqingActivity.class),
            new DemoInfo("国家图书馆关于在线使命认证服务暂停的公告","2020-07-09",R.drawable.gonggao,gonggaoxianqingActivity.class),
    };

    private class DemoListAdapter extends BaseAdapter {
        public DemoListAdapter() {
            super();
        }
        @Override
        public View getView(int index, View convertView, ViewGroup parent) {
            convertView = View.inflate(guotugonggaoActivity.this,
                    R.layout.guotugonggao_item, null);
            ImageView img_gonggao = convertView.findViewById(R.id.gonggao_b1);
            TextView t1 = convertView.findViewById(R.id.gonggao_t1);
            TextView t2 = convertView.findViewById(R.id.gonggao_t2);
            img_gonggao.setImageResource(demos[index].imageId);
            t1.setText(demos[index].name1);
            t2.setText(demos[index].name2);
            return convertView;
        }

        @Override
        public int getCount() {
            return demos.length;
        }

        @Override
        public Object getItem(int index) {
            return demos[index];
        }

        @Override
        public long getItemId(int id) {
            return id;
        }
    }

    private static class DemoInfo {
        private String name1;
        private String name2;
        private int imageId;
        private final Class<? extends android.app.Activity> demoClass;

        public DemoInfo(String name1,String name2, int imageId,
                        Class<? extends android.app.Activity> demoClass) {
            this.name1 = name1;
            this.name2 = name2;
            this.imageId = imageId;
            this.demoClass = demoClass;
        }
    }
}
